package org.cheniy.domain.strategy.service.rule.tree.impl;

import lombok.extern.slf4j.Slf4j;
import org.cheniy.domain.strategy.model.valobj.RuleLogicCheckTypeVO;
import org.cheniy.domain.strategy.service.rule.tree.ILogicTreeNode;
import org.cheniy.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import org.cheniy.types.common.Constants;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author cheniy
 * @description 抽奖幸运奖树过滤节点
 * @create 2025/3/8 15:32
 */

@Slf4j
@Component("rule_luck_award")
public class RuleLuckAwardLogicTreeNode implements ILogicTreeNode {
    @Override
    public DefaultTreeFactory.TreeActionEntity logic(String userId, Long strategyId, Integer awardId, String ruleValue, Date endDateTime) {
        log.info("规则过滤-兜底奖品 userId:{} strategyId:{} awardId:{} ruleValue:{}",userId, strategyId,awardId,ruleValue);

        // ruleValue：101:1,100（兜底奖品：）
        String[] split = ruleValue.split(Constants.COLON);
        if (split.length==0){
            log.error("规则过滤-兜底奖品,兜底奖品未配置告警 userId:{} strategyId:{} awardId:{}",userId,strategyId,awardId);
            throw new RuntimeException("数据库兜底奖品未配置");
        }

        // 兜底奖品配置
        Integer luckAwardId = Integer.valueOf(split[0]);
        String awardRuleValue = split.length > 1 ? split[1] : "";
        log.info("规则过滤-兜底奖品 userId:{} strategyId:{} awardId:{} awardRuleValue：{}",userId,strategyId,awardId,awardRuleValue);

        // 传出兜底奖品
        return DefaultTreeFactory.TreeActionEntity.builder()
                .ruleLogicCheckType(RuleLogicCheckTypeVO.TAKE_OVER)
                .strategyAwardVO(DefaultTreeFactory.StrategyAwardVO.builder()
                        .awardId(luckAwardId)
                        .awardRuleValue(awardRuleValue)
                        .build())
                .build();
    }
}
